﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SobrietyEngine.Components;
using SobrietyEngine.Actor;

namespace ActorTemplateEditor
{
    public partial class EditorForm : Form
    {

        ActorLibrary actorLibrary;

        public EditorForm()
        {
            InitializeComponent();

            //create components

            //load sobriety engine default components
            RegisterComponentsWithFactory.Register();

            actorLibrary = new ActorLibrary();
        }

        private void componentListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ComponentListForm clf = new ComponentListForm();
            clf.Show();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void actorListCtxMenu_Opening(object sender, CancelEventArgs e)
        {

        }

        private void newActorCtxMnu_Click(object sender, EventArgs e)
        {

        }

        private void removeActorCtxMnu_Click(object sender, EventArgs e)
        {

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "xml";
            if (ofd.ShowDialog() != DialogResult.OK)
                return;
            
            //load the actor actorDefinition file
            String file = ofd.FileName;

            actorLibrary.Clear();
            actorLibrary.Load(file);

            //display actors in tree
            List<TreeNode> allNodes = new List<TreeNode>();

            TreeNode root = new TreeNode("Actors");
            allNodes.Add(root);

            foreach (String actorName in actorLibrary.Templates.Keys)
            {
                ActorTemplate at = actorLibrary.Templates[actorName];
                TreeNode tn = new TreeNode(actorName);
                tn.Tag = at;
                allNodes.Add(tn);

                //lets see if we were iherited from anyone
                if (at.InheritedFrom != String.Empty && at.InheritedFrom != null)
                {
                    //lets find this actor in the list:
                    foreach (TreeNode node in allNodes)                    
                        if ( ((ActorTemplate)node.Tag).Name == at.InheritedFrom)                        
                            node.Nodes.Add(tn);
                }
                else
                    root.Nodes.Add(tn);                
            }
            treeView1.Nodes.Add(root);

            foreach (TreeNode n in allNodes)
                n.Expand();
        }
    
    
    
    }
}
